<template>
  <div>
    <el-table :data="tableData" @sort-change="sortChange">
      <el-table-column prop="username" label="会员名"></el-table-column>
      <el-table-column prop="teach" label="教练"></el-table-column>
      <el-table-column prop="traintimes" label="训练次数" sortable="custom"></el-table-column>
      <el-table-column prop="paytimes" label="办卡次数" sortable="custom"></el-table-column>
      <el-table-column prop="payamount" label="消费总额" sortable="custom"></el-table-column>
      <el-table-column prop="type" label="会员类型"></el-table-column>
      <el-table-column prop="description" label="客户描述"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="会员详情" :visible.sync="dialogVisible">
      <el-form :model="currentMember">
        <el-form-item label="会员名">
          <el-input v-model="currentMember.username" readonly></el-input>
        </el-form-item>
        <el-form-item label="教练">
          <el-input v-model="currentMember.teach" readonly></el-input>
        </el-form-item>
        <el-form-item label="训练次数">
          <el-input v-model="currentMember.traintimes" readonly></el-input>
        </el-form-item>
        <el-form-item label="办卡次数">
          <el-input v-model="currentMember.paytimes" readonly></el-input>
        </el-form-item>
        <el-form-item label="消费金额">
          <el-input v-model="currentMember.payamount" readonly></el-input>
        </el-form-item>
        <el-form-item label="会员类型">
          <el-input v-model="currentMember.type" readonly></el-input>
        </el-form-item>
        <el-form-item label="客户描述">
          <el-input type="textarea" v-model="currentMember.description" readonly></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      currentMember: {
        username: "",
        paytimes: "",
        payamount: "",
        type: "",
        description: "",
        teach: "",
        traintimes: ""
      }
    };
  },
  methods: {
    handleEdit(row) {
      this.currentMember = { ...row };
      this.dialogVisible = true;
    },
    sortChange({ prop, order }) {
      this.tableData.sort((a, b) => {
        const valueA = a[prop];
        const valueB = b[prop];
        if (order === "ascending") {
          return valueA - valueB;
        } else {
          return valueB - valueA;
        }
      });
    }
  },
  created() {
    this.$axios
      .get("http://47.108.248.93:999/consume", {
        headers: {
          token: localStorage.getItem("token")
        }
      })
      .then(res => {
        console.log(res.data.data);
        this.tableData = res.data.data;
      });
  }
};
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>
